package com.zkthink.ceres.member.service.impl;

import com.zkthink.base.R;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.member.dao.MemberAuthenticationMapper;
import com.zkthink.ceres.member.dao.MemberMapper;
import com.zkthink.ceres.member.dao.MemberRecommendMapper;
import com.zkthink.ceres.member.dto.MemberAuthenticationPageDTO;
import com.zkthink.ceres.member.dto.MemberAuthenticationSaveDTO;
import com.zkthink.ceres.member.dto.MemberAuthenticationUpdateDTO;
import com.zkthink.ceres.member.entity.MemberAuthentication;
import com.zkthink.ceres.member.service.MemberAuthenticationService;
import com.zkthink.ceres.member.utils.CheckIdCard;
import com.zkthink.ceres.member.vo.MemberAuthenticationVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@AllArgsConstructor
public class MemberAuthenticationServiceImpl extends SuperServiceImpl<MemberAuthenticationMapper, MemberAuthentication> implements MemberAuthenticationService {

//    @Autowired
//    private MemberDiscountMapper memberDiscountMapper;

    @Autowired
    private MemberAuthenticationMapper memberAuthenticationMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberRecommendMapper memberRecommendMapper;

    @Override
    public boolean saveBatchSomeColumn(List<MemberAuthentication> entityList) {

        return false;
    }

    @Override
    public R<Boolean> saveMemberAuthentication(MemberAuthenticationSaveDTO dto) {
        boolean check = CheckIdCard.check(dto.getIdCard());
        if (!check) {
            R.fail("身份证信息填写错误");
        }
        MemberAuthentication memberAuthentication1 = memberAuthenticationMapper.searchMemberAuthenticationByMid(dto.getMid());
        if (memberAuthentication1 == null) {
            MemberAuthentication memberAuthentication = new MemberAuthentication();
            BeanUtils.copyProperties(dto, memberAuthentication);
            memberAuthentication.setStatus(1);
            memberAuthenticationMapper.insert(memberAuthentication);
        } else {
            BeanUtils.copyProperties(dto, memberAuthentication1);
            memberAuthentication1.setStatus(1);
            baseMapper.updateById(memberAuthentication1);
        }
        return R.success();
    }

    @Override
    public R<Boolean> updateMemberAuthentication(MemberAuthenticationUpdateDTO dto) {
        //根据审核是否通过，计算是否增加贡献值
        //原来状态
        Integer oldStatus = searchMemberAuthenticationByMid(dto.getMid()).getData().getStatus();
        //
        MemberAuthentication memberAuthentication = new MemberAuthentication();
        BeanUtils.copyProperties(dto, memberAuthentication);
        memberAuthenticationMapper.updateById(memberAuthentication);
        return R.success();
    }

    @Override
    public R<Boolean> deleteMemberAuthentication(MemberAuthenticationUpdateDTO dto) {
        return null;
    }

    @Override
    public R<MemberAuthenticationVO> searchMemberAuthentication(MemberAuthenticationPageDTO dto) {
        List<MemberAuthentication> memberAuthentications = memberAuthenticationMapper.selectMemberAuthentication(dto);
        Integer total = memberAuthenticationMapper.selectMemberAuthenticationCount(dto);
        MemberAuthenticationVO vo = new MemberAuthenticationVO();
        vo.setMemberAuthenticationVO(memberAuthentications);
        vo.setTotal(total);
        return R.success(vo);
    }

    @Override
    public R<MemberAuthentication> searchMemberAuthenticationByMid(Long mid) {
        return R.success(memberAuthenticationMapper.searchMemberAuthenticationByMid(mid));
    }


}
